package com.fau.hm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.hm.action.PublicAction;
import com.fau.hm.entity.MedicalAdmin;
import com.fau.hm.entity.MedicalPlatform;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 平台(中心)信息维护Action
 * @author dangjh
 * @datetime 2014年10月14日,下午10:54:09
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class PlatformAction extends PublicAction implements ModelDriven<MedicalPlatform> {

	private static final long serialVersionUID = 32007810302187786L;
	
	private MedicalPlatform platform = new MedicalPlatform();
	
	/**
	 * @desc 实例化平台(中心)列表页
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月12日,下午6:20:00
	 */
	@Action(value = "init_platform_list", results = {@Result(name = SUCCESS, location = "init_platform_list.jsp")})
	public String initPlatformList(){
		return SUCCESS;
	}

	/**
	 * @desc 平台(中心)数据列表展示
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午9:39:01
	 */
	@Action(value = "platform_list_view")
	public String platformListView(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getDeptService().findPlatformByEid(map, platform, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 实例平台(中心)信息添加界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午9:52:23
	 */
	@Action(value = "medical_platform_add", results = {@Result(name = SUCCESS, location = "medical_platform_add.jsp")})
	public String platformAdd(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存平台(中心)信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午10:29:51
	 */
	@Action(value = "medical_platform_save")
	public String platformSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		boolean flag = this.baseAllService.getDeptService().savePlatform(platform, admin);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 打开平台(中心)信息修改界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午9:52:23
	 */
	@Action(value = "medical_platform_edit", results = {@Result(name = SUCCESS, location = "medical_platform_edit.jsp")})
	public String platformEdit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存编辑的平台(中心)信息 
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午10:30:54
	 */
	@Action(value = "medical_platformedit_save")
	public String platformEditSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(platform.getId())){
			boolean flag = this.baseAllService.getDeptService().saveEditPlatform(platform);
			if(flag){
				map.put("success", "true");
			}else{
				map.put("success", "false");
			}
		}else{
			map.put("success", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 平台(中心)下是否包含组
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月15日,上午11:34:10
	 */
	@Action(value = "platform_include_group")
	public String includeGroup(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(platform.getId())){
			boolean flag = this.baseAllService.getDeptService().includePlatfromGroup(platform.getId());
			if(flag){
				map.put("success", "true");
			}
		}else{
			map.put("success", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除平台(中心)
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月15日,上午11:33:08
	 */
	@Action(value = "meidcal_platform_del")
	public String platformDel(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(platform.getId())){
			this.baseAllService.getDeptService().delPlatform(platform);
			map.put("success", "true");
		}else{
			map.put("success", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 验证同一个中心下是否已存在当前添加的组
	 * @return null
	 * @author dangjh
	 * 2014年12月24日 下午4:56:40
	 */
	@Action(value = "medical_include_center")
	public String includePlatform() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = new HashMap<String, Object>();
		boolean flag = this.baseAllService.getDeptService().includePlatform(admin.getEnterpriseId(), platform.getName(), platform.getPlatformType());
		if(!flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalPlatform getModel() {
		return platform;
	}

}
